/*
  The MIT License
  
  Copyright (c) 2010 smath-view project
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/
package pl.smath.view.renderer;

import gwt.g2d.client.graphics.Surface;
import pl.smath.expression.model.FractionTerm;
import pl.smath.expression.model.ProductTerm;
import pl.smath.expression.model.RootTerm;
import pl.smath.expression.model.SumTerm;
import pl.smath.expression.model.Term;
import pl.smath.expression.model.NumberTerm;
import pl.smath.expression.model.VariableTerm;
import pl.smath.expression.parser.ExpressionParser;
import pl.smath.expression.parser.ExpressionParserException;
import pl.smath.view.model.FractionView;
import pl.smath.view.model.NumberView;
import pl.smath.view.model.ProductView;
import pl.smath.view.model.RootView;
import pl.smath.view.model.SumView;
import pl.smath.view.model.VariableView;
import pl.smath.view.model.View;
import pl.smath.view.utils.Cursor;
import pl.smath.view.utils.Rectangle;

/**
 * ViewManager - manages the view that represents certain Term.
 * 
 * @author Damian Rodziewicz
 *
 */
public class ViewManager {
	private final Term term;
	private View termView;
	private Cursor startPosition;
	private String font;

	/**
	 * Constructor
	 */
	public ViewManager(Term term) {
		this.term = term;
	}
	
	/**
	 * Constructor
	 * @throws ExpressionParserException 
	 */
	public ViewManager(String expression) throws ExpressionParserException {
		ExpressionParser parser = new ExpressionParser();
		Term term = parser.parseText(expression);
		
		this.term = term;
	}
	
	/**
	 * Generates TermView class from term to be able to operate on later. 
	 * @throws TermRendererException 
	 */
	public View createView(Term term) throws TermRendererException {
		View view = null;
		
		if (term instanceof NumberTerm) {
			view = new NumberView(term.toString());
			return view;
		}
		
		if (term instanceof VariableTerm) {
			view = new VariableView(term.toString());
			return view;
		}
		
		if (term instanceof SumTerm) {
			view = new SumView();
			for (Term termElement : ((SumTerm) term).getTerms()) {
				((SumView) view).addView(createView(termElement));
			}
			view.setNegative(term.isNegative());
			return view;
		}
		
		if (term instanceof ProductTerm) {
			view = new ProductView();
			for (Term termElement : ((ProductTerm) term).getTerms()) {
				((ProductView) view).addView(createView(termElement));
			}
			view.setNegative(term.isNegative());
			return view;
		}
		
		if (term instanceof FractionTerm) {
			View numerator = createView(((FractionTerm) term).getNumerator());
			View denominator = createView(((FractionTerm) term).getDenominator());
			view = new FractionView(numerator, denominator);
			
			view.setNegative(term.isNegative());
			return view;
		}
		
		if (term instanceof RootTerm) {
			View degree = createView(((RootTerm) term).getDegree());
			View base = createView(((RootTerm) term).getBase());
			view = new RootView(base, degree);
			
			view.setNegative(term.isNegative());
			return view;
		}
		
		throw new TermRendererException();
	}
	
	public void createView() throws TermRendererException {
		this.termView = this.createView(this.term);
		
		if (this.font == null) throw new TermRendererException();
		
		this.termView.setFont(this.font);
	}
	
	/**
	 * get font
	 * @return font
	 */
	public String getFont() {
		return this.font;
	}

	/**
	 * get the start position
	 * @return startPosition
	 */
	public Cursor getStartPosition() {
		return startPosition;
	}

	/**
	 * Renders and returns canvas with use of TermView.
	 * @return Canvas
	 * @throws TermRendererException 
	 */
	public Surface renderCanvas() throws TermRendererException {
		Rectangle rectangle = termView.getRectangle();
		Surface returnCanvas = new Surface((int)(rectangle.getWidth()+0d), (int)(rectangle.getHeight()+10d));
		if (this.font == null) this.setFont(returnCanvas.getFont());
		else returnCanvas.setFont(this.font);
		
		//TODO set cursor
		Cursor cursor = new Cursor(0d, termView.getHeight());
		
		termView.setCursor(cursor);
		termView.render(returnCanvas);
		
		return returnCanvas;
	}

	/**
	 * sets font
	 * @param font
	 */
	public void setFont(String font) {
		this.font = font;
		if (this.termView != null) {
			this.termView.setFont(font);
		}
	}

	/**
	 * sets the position to start drawing
	 * @param startPosition
	 */
	public void setStartPosition(Cursor startPosition) {
		this.startPosition = startPosition;
		this.termView.setCursor(new Cursor(startPosition.getX(), startPosition.getY()));
	}
	
	public View getView() {
		return this.termView;
	}
}
